#include "browserwindow.h"
#include <QtWebEngineWidgets>
#include <QTabWidget>
#include "mydockwidget.h"
#include "mywebview.h"
#include "mytabwidget.h"
#include "mytitlebar.h"
#include <QVBoxLayout>
#include "mytoolbar.h"
BrowserMainWindow::BrowserMainWindow(QWidget *parent)
    : QMainWindow(parent)
    , m_pTabwidget(nullptr)
    , m_pmenubar(nullptr)
    , m_pMainWidget(nullptr)
{
    setMinimumSize(800, 600);
    setWindowFlags(Qt::FramelessWindowHint); // 无

    //setMenuWidget(m_pmenubar);

    m_pMainWidget = new QWidget(this);
    QVBoxLayout* m_pMainLayout = new QVBoxLayout(this);

    m_pmenubar = new MyTitleBar(this);
    connect(m_pmenubar, &MyTitleBar::minimize, this, &QMainWindow::showMinimized);
    connect(m_pmenubar, &MyTitleBar::toggleMaximize, this, [this]() {
        if (isMaximized())
            showNormal();
        else
            showMaximized();
    });
    m_pmenubar->setMinimumHeight(30);
    connect(m_pmenubar, &MyTitleBar::close, this, &QMainWindow::close);
    connect(m_pmenubar, &MyTitleBar::sigAddWeb, this, &BrowserMainWindow::slotAddWeb);

    MyToolBar* m_pToolBar = new MyToolBar(this);
    m_pMainLayout->addWidget(m_pmenubar);
    //m_pMainLayout->addSpacing(-12);
    m_pMainLayout->addWidget(m_pToolBar);

    m_pTabwidget = new QTabWidget (this);
    Mywebview* m_pweb = new Mywebview(m_pTabwidget);
    m_pweb->load(QUrl("https://www.baidu.com"));
    m_pTabwidget->setDocumentMode(true);

    Mywebview* m_pweb1 = new Mywebview(m_pTabwidget);
    m_pweb1->load(QUrl("https://www.baidu.com"));

    connect(m_pTabwidget, &QTabWidget::tabCloseRequested, this, &BrowserMainWindow::slottabCloseRequested);
    connect(m_pTabwidget, &QTabWidget::tabBarDoubleClicked, this, &BrowserMainWindow::tabBarDoubleClicked);
    m_pTabwidget->setTabsClosable(true);
    m_pTabwidget->addTab(m_pweb, "11111");
    m_pTabwidget->addTab(m_pweb1, "22222");

    m_pMainLayout->addWidget(m_pTabwidget);

    m_pMainWidget->setLayout(m_pMainLayout);
    setCentralWidget(m_pMainWidget);
}

BrowserMainWindow::~BrowserMainWindow()
{

}

void BrowserMainWindow::paintEvent(QPaintEvent *event)
{
    // QPainter painter(this);
    // painter.fillRect(QRect(0,0,m_pmenubar->width(), m_pmenubar->height()), Qt::blue);
}

void BrowserMainWindow::resizeEvent(QResizeEvent *event)
{

}

void BrowserMainWindow::slottabCloseRequested(int nIndex)
{
    m_pTabwidget->removeTab(nIndex);
}

void BrowserMainWindow::tabBarDoubleClicked(int nIndex)
{

}

void BrowserMainWindow::slotAddWeb()
{
    m_pmenubar->addTitleBar();
}


